const { Controller } = require('egg');

class BeerRecordController extends Controller {
  async list() {
    const { ctx } = this;
    const token = ctx.request.header.authorization.replace('Bearer ', '');
    // 解码token
    const decode = ctx.app.jwt.verify(token, 'beermaster');

    const detail = await ctx.service.beerRecord.list({
      open_id: decode.open_id,
    });
    // const detail = await ctx.service.beerRecord.add({ name: 'test' });
    ctx.body = {
      data: detail,
    };
  }

  async detail() {
    const { ctx } = this;
    const detail = await ctx.service.beerRecord.detail(ctx.params.id);
    ctx.body = {
      code: 0,
      data: detail,
    };
  }

  async add() {
    const { ctx } = this;
    const { body } = ctx.request;

    const token = ctx.request.header.authorization;
    // 解码token
    const decode = ctx.app.jwt.verify(token, 'beermaster');
    console.log('decode', decode);

    const detail = await ctx.service.beerRecord.add(body);
    ctx.body = {
      code: 0,
      data: detail,
    };
  }
}

module.exports = BeerRecordController;
